var blockdata=new Array(),block=false,white=new Array("newtab"),blockedId=new Array(),ver=1.1;
function initset(res)
{
	if(res.on)localStorage["on"]=res.on; else localStorage["on"]="on";
	if(res.off)localStorage["off"]=res.off; else localStorage["off"]="off";
	if(res.alt)localStorage["alt"]=res.alt; else localStorage["alt"]="false";
}
function setblock(st)
{
	block=st;
	chrome.browserAction.setBadgeBackgroundColor({color:(block?"#ff0000":"#00ff00")});
	chrome.browserAction.setBadgeText({text:(block?((blockdata.length==0)?
			((localStorage.on)?localStorage.on:"on"):
			blockdata.length.toString()):
		((localStorage.off)?localStorage.off:"off"))});
}

chrome.extension.onRequest.addListener(
	function(request,sender,response)
	{
		switch(request.info)
		{
			case "blocked":     response({reply:blockdata}); break;
			case "turn":        setblock(request.val);       break;
			case "getstat":     response({reply:block});     break;
			case "getwhite":    response({reply:white});     break;
			case "changewhite": white=request.val;           break;
			case "refbdg":      setblock(block);             break;
			case "addwhite":    white.push(request.val);     break;
			case "open":        if(block){block=false;
					    chrome.tabs.create({url:request.url},function(){block=true});}
					    break;
			case "alert":       if(block&&localStorage.alt=="true")
					    response({should:true});
					    break;
		}
	}
)

chrome.tabs.onCreated.addListener(
	function(tab)
	{
		if(block)
		{		
			for(i in white){if(white[i]==tab.url.split("/")[2])return;}			
			blockdata.push(tab.url);
			chrome.tabs.remove(tab.id);
			chrome.browserAction.setBadgeText({text:blockdata.length.toString()});
			tempon=false;
		}
	}
);

window.onload=function(){setblock(block);chrome.storage.sync.get(["on","off"],initset);}

document.head.innerHTML+="<link rel=icon href=icon.png />"
var menu_add_and_open=chrome.contextMenus.create({title:"在新标签页中打开",contexts:["link"],onclick:function(info,tab){if(block){
	block=false;
	}
	chrome.tabs.create({url:info.linkUrl},function(){block=true});
	}})
